#define _CRT_SECURE_NO_WARNINGS
#include "Queue.h"
namespace bit
{


	void QueueInit(Queue* pq)
	{
		assert(pq);

		pq->patil = NULL;
		pq->phead = NULL;
		pq->size = 0;
	}

	void QueueDestroy(Queue* pq)
	{
		assert(pq);
		QNode* pcur = pq->phead;

		while (pcur)
		{
			QNode* next = pcur->next;
			free(pcur);
			pcur = next;
		}
		pq->patil = pq->phead = NULL;
		pq->size = 0;
	}

	void QueueFush(Queue* pq, QDataType x)
	{
		assert(pq);
		QNode* newnode = (QNode*)malloc(sizeof(QNode));
		if (newnode == NULL)
		{
			perror("malloc fail");
			exit(1);
		}
		newnode->data = x;
		newnode->next = NULL;

		if (pq->patil == NULL)
		{
			assert(pq->phead == NULL);
			pq->phead = pq->patil = newnode;
		}
		else
		{
			pq->patil->next = newnode;
			pq->patil = pq->patil->next;
		}
		pq->size++;
	}

	void QueuePop(Queue* pq)
	{
		assert(pq);
		assert(!QueueEmpty(pq));

		if (pq->phead->next == NULL)
		{
			free(pq->phead);
			pq->phead = pq->patil = NULL;
		}
		else
		{
			QNode* next = pq->phead->next;
			free(pq->phead);
			pq->phead = next;
		}
		pq->size--;
	}

	QDataType QueueFront(Queue* pq)
	{
		assert(pq);
		assert(!QueueEmpty(pq));

		return pq->phead->data;
	}

	QDataType QueueBack(Queue* pq)
	{
		assert(pq);
		assert(!QueueEmpty(pq));

		return pq->patil->data;
	}

	bool QueueEmpty(Queue* pq)
	{
		assert(pq);

		return pq->phead == NULL && pq->patil == NULL;
	}

	int QueueSize(Queue* pq)
	{
		assert(pq);

		return pq->size;
	}
}